"""Helper functions

Consists of functions to typically be used within templates, but also available
to Controllers. This module is available to both as 'h'.
"""
from webhelpers import *

def format_options(list, value_prop='id', caption_prop='name',\
                   selected_value=None, add_empty=False, empty_caption=''):
    result = '' if not add_empty else '<option value="">%s</option>' % empty_caption
    for item in list:
        result += '<option %s %s>%s</option>\n' %\
         ('value="%s"' % getattr(item, value_prop) if value_prop != None else '',\
          'selected="selected"' if selected_value != None and str(getattr(item, value_prop)) == str(selected_value) else '',\
          getattr(item, caption_prop))
    return result